package com.ygo.feihua.util;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.graphics.*;
import android.os.*;
import android.view.View.*;
import android.view.inputmethod.*;
import android.widget.*;
import android.widget.AdapterView.*;
import android.widget.LinearLayout.*;
import cn.bmob.v3.*;
import cn.bmob.v3.datatype.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.baidu.mapapi.radar.*;

import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.adapter.*;


import android.view.View.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.support.v4.util.*;
import android.util.DisplayMetrics;
import java.text.*;
import android.util.Log;
import android.support.design.widget.*;
import android.support.v4.view.*;
import android.support.v4.app.*;
import android.support.v7.app.*;
import android.view.*;
import android.app.Dialog;
import android.app.Activity;
import android.app.Service;
import com.ygo.feihua.fragment.*;
import com.ygo.feihua.logo.*;
import com.qnw.CardGroupManagement.*;
import com.qnw.CardGroupManagement.util.*;
import com.qnw.CardGroupManagement.adapter.*;
import android.support.v7.widget.*;
import com.ygo.feihua.service.*;
import com.qnw.CardGroupManagement.fragment.*;
/*对话框相关方法
*
*/
public class DialogUtils
{
	//对话框中提示内容
	private TextView dj_ts;
	//登录对话框三条分割线
	private View v1,v2,v3;
	private TextView yhzc_denglu,yhzc_zc,yhzc_zmm;
	//登录对话框三个编辑框
	private EditText yhed_name,yhed_email,yhed_password;
	//登录对话框登录按钮
	private Button yhzc_qd;
	//登录对话框三个EditText的父layout
	private LinearLayout yhzc_lname,yhzc_lpassword,yhzc_lemail;
	
	//对话框对象
	private Dialog builder;
	private Context context;
	private static List<DialogUtils> contexts=new ArrayList<DialogUtils>();

	//登录对话框状态
	int login_v=1;

	//private Gongju gj;

	public Context getContext()
	{
		return context;
	}
	
	//获取该类对象
	public static DialogUtils getdx(Context context){
		DialogUtils dut= getDu(context);
		if(dut!=null){		
		return dut;
		}else{
			
			DialogUtils du=new DialogUtils(context);
			contexts.add(du);
			return du;
		}
	}
	
	private static DialogUtils getDu(Context con){
		for( DialogUtils du:contexts){
			if(du.getContext().equals(con)){
				return du;
			}
		}
		return null;
	}
	
	private DialogUtils(Context context){
		this.context=context;
	}
	
	//关闭Dialog
	public void dis(){
		builder.dismiss();		
	}
	
	
	private Dialog initDialog()
	{
		if (builder == null){
			builder = new Dialog(context, R.style.dialog);
			//去除原dialog标题
			builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
		}
		if (context instanceof Service){
			builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		}else{
			builder.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
		}

		builder.show();
		return builder;
	}

	/*
	 *初始化对话框布局
	 *传入对话框的layout
	 *返回该布局
	 */
	private View initDialog(int layout)
	{
		initDialog();

		LayoutInflater inflater = LayoutInflater.from(context);
		View viewDialog = inflater.inflate(layout, null);	
		int width;
		if(context instanceof Activity){
			Activity a=(Activity)context;
			Display display = a.getWindowManager().getDefaultDisplay();
			width=display.getWidth();
		}else{
			DisplayMetrics dm = new DisplayMetrics();
			Service se=(Service) context;
			dm = se.getResources().getDisplayMetrics();
			width = dm.widthPixels;
		}
		//设置对话框的宽高
		LayoutParams layoutParams = new LayoutParams(width * 73 / 100,LayoutParams.WRAP_CONTENT);
		builder.setContentView(viewDialog, layoutParams);
		return viewDialog;
		// TODO: Implement this method
	}
	
	

	public final static int QUERY_ALL=0;
	public final static int QUERY_CARD=1;
	public final static int QUERY_DECK=2;
	public final static int QUERY_ZHUTI=3;
	public final static int QUERY_KADIAN=4;
	//提示对话框
	public void dialogMessagQuery(int[] query_type,FragmentManager fm){
		
		List<Map<String,Object>> fg=new ArrayList<Map<String,Object>>();
		for(int i:query_type){
			int type=QUERY_CARD;
			String title="";
			switch(i){		
				case QUERY_CARD:
					type=QUERY_CARD;
					title="卡片";
					break;
				case QUERY_DECK:
					type=QUERY_DECK;
					title="卡组";
					
					break;
				case QUERY_ZHUTI:
					type=QUERY_ZHUTI;
					title="主题";
					
					break;
				case QUERY_KADIAN:
					type=QUERY_KADIAN;
					title="卡店";
					break;
			}
			FragmentQueryList f1=new FragmentQueryList();
			Bundle bundle1 = new Bundle();  
			bundle1.putInt("type", type);  
			f1.setArguments(bundle1);  
			Map<String,Object> map1=new ArrayMap<String,Object>();
			map1.put("fr",f1);
			map1.put("title",title);
			fg.add(map1);	
			
		}
		if(fg.size()==0){
			return;
		}
		MessageQueryDialog mqd=new MessageQueryDialog(fg);
		
		mqd.show(((AppCompatActivity)context).getSupportFragmentManager(),"MessageQueryDialog");
		
	}
	
	
	
	
	public interface OnRadioListener{
		void onRadio(List<String> data,int position);
	}
	public interface OnCheckboxListener{
		void OnCheckbox(List<String> data,List<Integer> positons);
	}
	
	public class Select{
		private OnRadioListener onr;
		private OnCheckboxListener onc;
		
		public void setOnRadioListener(OnRadioListener onr){
			this.onr=onr;
		}
		public void setOnCheckboxListener(OnCheckboxListener onc){
			this.onc=onc;
		}
	}
	
	//多选对话框
	public Select dialogCheckbox(String title,final List<String> data,final List<Integer> position){


		final Select se=new Select();
		View viewDialog=initDialog(R.layout.dialog_select);
		TextView ds_title=(TextView) viewDialog.findViewById(R.id.ds_title);
		Button ds_qd=(Button) viewDialog.findViewById(R.id.ds_qd);
		Button ds_qx=(Button) viewDialog.findViewById(R.id.ds_qx);
		ListView ds_list=(ListView) viewDialog.findViewById(R.id.ds_list);

		ds_title.setText(title);
		
		final SelectAdapter sa=new SelectAdapter(context,data,position);
		ds_list.setAdapter(sa);
		if(position.size()!=0){
			ds_list.setSelection(position.get(0));
		}
		ds_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
				
						if(conin(position,p3)){
							removein(position,p3);
						}else{
							position.add(new Integer(p3));
						}
						sa.notifyDataSetChanged();
					
					// TODO: Implement this method
				}
			});
		ds_qx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					// TODO: Implement this method
				}
			});
		ds_qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					
					if(se.onc!=null){
						se.onc.OnCheckbox(data,position);
						dis();
					}
					// TODO: Implement this method
				}
			});

		    builder.setCanceledOnTouchOutside(true);
		return se;	
	}
	
	private void removein(List<Integer> data,int c){
		for(int i=0;i<data.size();i++ ){
			if(data.get(i)==c){
				data.remove(c);
				return;
			}
		}
	}
	
	private boolean conin(List<Integer> data,int c){
		for(int cc:data){
			if(cc==c){
				return true;
			}
		}
		return false;
	}
	
	//单选对话框
	public Select dialogRadio(String title,final List<String> data,int position){

		
		final Select se=new Select();
		View viewDialog=initDialog(R.layout.dialog_select);
		TextView ds_title=(TextView) viewDialog.findViewById(R.id.ds_title);
		Button ds_qd=(Button) viewDialog.findViewById(R.id.ds_qd);
		Button ds_qx=(Button) viewDialog.findViewById(R.id.ds_qx);
		ListView ds_list=(ListView) viewDialog.findViewById(R.id.ds_list);
		
		ds_title.setText(title);
		final List<Integer> po=new ArrayList<Integer>();
		po.add(new Integer(position));
		final SelectAdapter sa=new SelectAdapter(context,data,po);
		ds_list.setAdapter(sa);
		if(position!=-1){
			ds_list.setSelection(position);
		}
		ds_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					if(po.size()!=0){
						if(po.get(0)!=p3){
							po.clear();
							po.add(new Integer(p3));
						}
						sa.notifyDataSetChanged();
					}else{
						po.add(new Integer(p3));
						sa.notifyDataSetChanged();
					}
					// TODO: Implement this method
				}
			});
		ds_qx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					// TODO: Implement this method
				}
			});
		ds_qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(po.size()!=0){
						if(se.onr!=null){
					se.onr.onRadio(data,po.get(0));
					}
					dis();
					}else{
					Gongju.show("请选择一项后确定");
					}
					// TODO: Implement this method
				}
			});
		
	   builder.setCanceledOnTouchOutside(true);
		return se;	
	}
	
	//登录对话框
    public View[] dialoglogin(){

        View[] vi=new View[1];
        View viewDialog=initDialog(R.layout.dialog_login);
		yhzc_qd=(Button) viewDialog.findViewById(R.id.dt_qd);

		final UserManagement um=UserManagement.getDx();
		v1=viewDialog.findViewById(R.id.v1);
		v2=viewDialog.findViewById(R.id.v2);
		v3=viewDialog.findViewById(R.id.v3);
		yhzc_zc=(TextView) viewDialog.findViewById(R.id.yhzc_zc);
		yhzc_denglu=(TextView) viewDialog.findViewById(R.id.yhzc_denglu);
		yhzc_zmm=(TextView) viewDialog.findViewById(R.id.yhzc_zmm);
		yhed_name=(EditText) viewDialog.findViewById(R.id.yhed_name);
		yhed_password=(EditText) viewDialog.findViewById(R.id.yhed_password);
		yhed_email=(EditText) viewDialog.findViewById(R.id.yhed_email);
		yhzc_lname=(LinearLayout) viewDialog.findViewById(R.id.yhzc_lname);
		yhzc_lpassword=(LinearLayout) viewDialog.findViewById(R.id.yhzc_lpassword);
		yhzc_lemail=(LinearLayout) viewDialog.findViewById(R.id.yhzc_lemail);


		v2.setVisibility(View.GONE);
		v3.setVisibility(View.GONE);
		yhzc_lemail.setVisibility(View.GONE);

		yhzc_denglu.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dengi();
					// TODO: Implement this method
				}
			});
		yhzc_zc.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					yhzc_qd.setText("注册");
					v1.setVisibility(View.GONE);
					v2.setVisibility(View.VISIBLE);
					v3.setVisibility(View.GONE);
					yhzc_lname.setVisibility(View.VISIBLE);
					yhzc_lpassword.setVisibility(View.VISIBLE);
					yhzc_lemail.setVisibility(View.VISIBLE);
					login_v=2;

					// TODO: Implement this method
				}
			});
		yhzc_zmm.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					yhzc_qd.setText("找回密码");
					v1.setVisibility(View.GONE);
					v2.setVisibility(View.GONE);
					v3.setVisibility(View.VISIBLE);
					yhzc_lname.setVisibility(View.GONE);
					yhzc_lpassword.setVisibility(View.GONE);
					yhzc_lemail.setVisibility(View.VISIBLE);
					login_v=3;

					// TODO: Implement this method
				}
			});
		yhzc_qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{

					final String name=yhed_name.getText().toString().trim();
					final String password=yhed_password.getText().toString().trim();
					final String email=yhed_email.getText().toString().trim();
					switch(login_v){
						case 1:
							if(name.equals("")||password.equals("")){
								Gongju.show("用户名或密码不能为空");
							}else{
								dialogj1(false,"","正在登录中,请稍等");
								BmobQuery<MyUser> qu=new BmobQuery<MyUser>();
								qu.addWhereEqualTo("username",name);
								qu.findObjects(new FindListener<MyUser>(){

										@Override
										public void done(List<MyUser> p1, BmobException p2)
										{
											if(p2==null){
												if(p1.size()==0){
													Gongju.show("没有这个用户哦");
													dis();
												}else{
													for(MyUser myu:p1){
														if(	myu.getUserzt()){
															final MyUser denglu=new MyUser();
															denglu.setUsername(name);
															denglu.setPassword(password);
															denglu.login( new SaveListener<MyUser>(){

																	@Override
																	public void done(MyUser p1, BmobException p2)
																	{
																		if(p2==null){
																			if(denglu.getEmailVerified()){
																				Gongju.show("登录成功");
																				dis();
																				um.sxUser();
																				dis();
																			}else{
																				um.logOutUser();
																				Gongju.show("用户未激活,请到邮箱中激活用户");
																				yhzc_qd.setText("没收到邮件？\n点击重新发送验证邮件");
																				login_v=4;
																				
																				//dis();
																			}
																		}else{
																			um.logOutUser();
																			Gongju.show("登录失败"+p2.getMessage());
																			dis();
																		}
																		// TODO: Implement this method
																	}

																});

														}else{
															Gongju.show("帐号被冻结了呢,冻结原因为："+myu.getDjyy());
															dis();
														}
													}
												}
											}else{
												um.logOutUser();
												Gongju.show("查询失败"+p2.getMessage());
												dis();

											}
											// TODO: Implement this method
										}
									});
							}
							break;
						case 2:

							if(name.equals("")||password.equals("")||email.equals("")){
								Gongju.show("用户名或密码,邮箱不能为空");
							}else{
								if(email.indexOf("@")!=-1&&email.indexOf(".com")!=-1){
									dialogj1(false,"","正在注册中,请稍等");
									MyUser zhuce=new MyUser();
									zhuce.setUsername(name);
									zhuce.setPassword(password);
									zhuce.setEmail(email);
									zhuce.setUserzt(true);
									zhuce.setUser_qx(0);
									zhuce.setShequ_qx(0);
									zhuce.setLiaotian_qx(0);
									zhuce.setUserpoint(new BmobGeoPoint(0,0));
									zhuce.setsignature("因为有你我才得以坚持下去");
									zhuce.signUp( new SaveListener<MyUser>(){

											@Override
											public void done(MyUser p1, BmobException p2)
											{
												if(p2==null){

													dis();
													Gongju.show("注册成功,请到邮箱中激活用户");
													//		dengi();

													yhzc_qd.setText("没收到邮件？\n点击重新发送验证邮件");

													login_v=4;

												}else{
													dis();
													Gongju.show("注册失败"+p2);
												}
												// TODO: Implement this method
											}
										});
								}else{
									Gongju.show("邮箱格式不正确");
								}
							}
							break;
						case 3:
							if(email.equals("")){
								Gongju.show("邮箱不能为空");
							}else{
								if(email.indexOf("@")!=-1&&email.indexOf(".com")!=-1){
									BmobUser.resetPasswordByEmail(email, new UpdateListener(){

											@Override
											public void done(BmobException p1)
											{
												if(p1==null){
													Gongju.show("邮件已经发送,请到"+email+"这个邮箱进行操作喵~");
												}else{
													Gongju.show("找回密码失败"+p1);

												}
												// TODO: Implement this method
											}
										});
								}else{
									Gongju.show("邮箱格式不正确");
								}
							}						
							break;
						case 4:
							if(email.equals("")){
								Gongju.show("邮箱不能为空");
							}else{
								BmobUser.requestEmailVerify(email, new UpdateListener() {
										@Override
										public void done(BmobException e) {
											if(e==null){
												Gongju.show("请求验证邮件成功，请到" + email + "邮箱中进行激活。");
											}else{
												Gongju.show("发送邮件失败失败:" + e.getMessage());
											}
										}
									});
							}
							break;
					}
					// TODO: Implement this method
				}
			});
        vi[0]=yhzc_qd;
		   builder.setCanceledOnTouchOutside(true);
        return vi;   
	}
	
	
	
/*卡片信息对话框
*b:activity还是service调用的,true为activity,否则为service
*km:卡片密码
*/
    public View[] dialogci(boolean b,final String km){

		final Gongju gj=Gongju.getdx(context);
        View[] v=new View[5];
        builder = new Dialog(context,R.style.dialog);
        //去除原dialog标题
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
		builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		
        builder.show();

		
        LayoutInflater inflater = LayoutInflater.from(context);
        View viewDialog = inflater.inflate(R.layout.dialog_card_information, null);
		int width ;
		if(b){
        Activity a=(Activity)context;
        Display display = a.getWindowManager().getDefaultDisplay();
		width=display.getWidth();
		}else{
			DisplayMetrics dm = new DisplayMetrics();
			Service se=(Service) context;
			dm = se.getResources().getDisplayMetrics();
		width = dm.widthPixels;
			
		}
      
        //设置对话框的宽高
        LayoutParams layoutParams = new LayoutParams(width * 80 / 100,LayoutParams.WRAP_CONTENT);
        builder.setContentView(viewDialog, layoutParams);
		
		final TextView dci_name,dci_km,dci_xg,dci_gs,dci_sx,dci_type,dci_dj,dci_zz,dci_atk,dci_def;
		Button dci_qd,dci_qx;
		final ImageView dci_kt;
		dci_name=(TextView) viewDialog.findViewById(R.id.dci_name);
		dci_km=(TextView) viewDialog.findViewById(R.id.dci_km);
		dci_xg=(TextView) viewDialog.findViewById(R.id.dci_xg);
		dci_gs=(TextView) viewDialog.findViewById(R.id.dci_gs);
		dci_sx=(TextView) viewDialog.findViewById(R.id.dci_sx);
		dci_type=(TextView) viewDialog.findViewById(R.id.dci_type);
		dci_zz=(TextView) viewDialog.findViewById(R.id.dci_zz);
		dci_dj=(TextView) viewDialog.findViewById(R.id.dci_dj);
		dci_atk=(TextView) viewDialog.findViewById(R.id.dci_atk);
		dci_def=(TextView) viewDialog.findViewById(R.id.dci_def);
		
		
		
		dci_kt=(ImageView) viewDialog.findViewById(R.id.dci_kt);
		dci_qd=(Button) viewDialog.findViewById(R.id.dci_qd);
		dci_qx=(Button) viewDialog.findViewById(R.id.dci_qx);
		
		dci_km.setText("["+km+"]");
		
	
		gj.setKt(context,dci_kt,km);	
		
		SQLiteDatabase sjk=SQLiteDatabase.openOrCreateDatabase(SharedPreferenceUtil.getCdbpath(), null);//数据库不存在则创建，存在则取得对象的实例
		
		Cursor datas=sjk.query("datas", new String[]{"id","ot","type","level","attribute","race","atk","def"}, "id = ?", new String[]{km}, null, null, null);
		Cursor texts=sjk.query("texts", new String[]{"id","name","desc"}, "id = ?", new String[]{km}, null, null, null);
		if (datas.getCount() != 0 && texts.getCount() != 0)
		{//获取集合的个数，如果不为0(没查到则为0)
			datas.moveToNext();//移动到第一行，默认是0，没有写这句，直接获取值会抛异常
			texts.moveToNext();
			dci_name.setText(texts.getString(1).trim());
			dci_xg.setText(texts.getString(2).trim());
			dci_atk.setText(datas.getString(6).trim());
			dci_def.setText(datas.getString(7).trim());
			 String lx=datas.getString(2).trim();
			String ss="";
			List<String> ls=CardMessage.getCardType(Integer.parseInt(datas.getString(2).trim()));
			for(String s:ls){
				ss+=s+"|";
			}
			ss=ss.substring(0,ss.length()-1);
			dci_type.setText(ss);
			dci_dj.setText(CardMessage.getLevel(Integer.parseInt( lx),datas.getString(3).trim()));// getLevel(Long.parseLong(datas.getString(3).trim())));		
			
			dci_sx.setText(CardMessage.getAttribute(Integer.toHexString(Integer.parseInt(datas.getString(4).trim()))));
			
			dci_gs.setText(CardMessage.getOT(datas.getString(1).trim()));
			
			dci_zz.setText(CardMessage.getRace(datas.getString(5).trim()));
			
			
			}
		dci_qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String ss="";
					String xx=dci_type.getText().toString();
					if(!(xx.contains("魔法") || xx.contains("陷阱"))){
						ss="等级:"+dci_dj.getText().toString()+"\n属性:"
							+dci_sx.getText().toString()+"\n种族:"
							+dci_zz.getText().toString();
					}
					
					
					gj.fzMessage(dci_name.getText().toString()
							+dci_km.getText().toString()+"\n归属:"
							+dci_gs.getText().toString()+"\n类型:"
							+xx+"\n"+ss
							+"\n"+"效果:"+dci_xg.getText().toString());
					
							Gongju.show("已复制到剪贴板");
						dis();
					// TODO: Implement this method
				}
			});
			
		dci_qx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					// TODO: Implement this method
				}
			});
		dci_name.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					context.startActivity(new Intent(context,A5.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("km",km));
					// TODO: Implement this method
				}
			});
		
		
 
       builder.setCanceledOnTouchOutside(true);
        return v;   
	}

//字段提示对话框
    public View[] dialogzd(boolean b,String title){

        View[] v=new View[5];
   		View viewDialog =initDialog(R.layout.dialog_ziduan);
		TextView dz_title=(TextView) viewDialog.findViewById(R.id.dz_title);
        EditText dz_zd1,dz_zd2,dz_zd3,dz_zd4;
		dz_zd1=(EditText) viewDialog.findViewById(R.id.dz_zd1);
		dz_zd2=(EditText) viewDialog.findViewById(R.id.dz_zd2);
		dz_zd3=(EditText) viewDialog.findViewById(R.id.dz_zd3);
		dz_zd4=(EditText) viewDialog.findViewById(R.id.dz_zd4);
        Button dz_qd=(Button) viewDialog.findViewById(R.id.dz_qd);
        if(b){
            dz_title.setText(title);
        }else{
            dz_title.setVisibility(View.GONE);
        }
        v[0]=dz_zd1;
		v[1]=dz_zd2;
		v[2]=dz_zd3;
		v[3]=dz_zd4;
		v[4]=dz_qd;
      builder.setCanceledOnTouchOutside(true);
        return v;   
	}
	
	//RecyclerView布局提示对话框
    public View[] dialogRec(boolean b,String title,RecyclerView.Adapter adp,RecyclerView.LayoutManager layout){

        View[] v=new View[2];
        View viewDialog=initDialog(R.layout.dialog_rec);
		TextView dr_title=(TextView) viewDialog.findViewById(R.id.dr_title);
        RecyclerView dr_rec= (RecyclerView) viewDialog.findViewById(R.id.dr_rec);
        Button dr_qd=(Button) viewDialog.findViewById(R.id.dr_qd);
        if(b){
            dr_title.setText(title);
        }else{
            dr_title.setVisibility(View.GONE);
        }
		
		dr_rec.setLayoutManager(layout);
		dr_rec.setAdapter(adp);


        v[0]=dr_rec;
		v[1]=dr_qd;
        builder.setCanceledOnTouchOutside(true);
        return v;   
	}
	
//表格布局提示对话框
    public View[] dialoggrid(boolean b,String title,ListAdapter adp,int numColumns){

        View[] v=new View[2];
        View viewDialog=initDialog(R.layout.dialog_grid);
		TextView dt_title=(TextView) viewDialog.findViewById(R.id.dt_title);
        GridView dt_grid= (GridView) viewDialog.findViewById(R.id.dt_grid);
        Button dt_qd=(Button) viewDialog.findViewById(R.id.dt_qd);
        if(b){
            dt_title.setText(title);
        }else{
            dt_title.setVisibility(View.GONE);
        }
		dt_grid.setNumColumns(numColumns);
		dt_grid.setAdapter(adp);
		
    
        v[0]=dt_grid;
		v[1]=dt_qd;
        builder.setCanceledOnTouchOutside(true);
        return v;   
	}

//combo提示对话框
    public void dialogComboXx(final List<Map<String,Object>> data,String message){

		View viewDialog=initDialog(R.layout.dialog_comboxiangxi);
        TextView dc_message=(TextView) viewDialog.findViewById(R.id.dc_message);
		ListView dc_list=(ListView) viewDialog.findViewById(R.id.dc_list);
		TextView dc_text=(TextView) viewDialog.findViewById(R.id.dc_text);
		Button dc_qd=(Button) viewDialog.findViewById(R.id.dc_qd);
		dc_message.setText(message);
		if(data.size()==0){
			dc_list.setVisibility(View.GONE);
			dc_text.setVisibility(View.VISIBLE);
		}else{
			dc_list.setVisibility(View.VISIBLE);
			dc_text.setVisibility(View.GONE);
		List<String> ll=new ArrayList<String>();
		for(Map<String,Object> map:data){
			
			ll.add((String)map.get("name"));
		}
		dc_list.setAdapter(new AdapterTextview(context,ll));
		dc_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					dis();
					
					Intent intent=new Intent(context,A5.class);
					intent.putExtra("km",(String)data.get(p3).get("km"));
					context.startActivity(intent);
					
					// TODO: Implement this method
				}
			});
		}
        dc_qd.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					// TODO: Implement this method
				}
			});
        builder.setCanceledOnTouchOutside(true);
         
	}
	
//查询卡片
	public CardList dialogCxCards(){
		
		final Gongju gj=Gongju.getdx(context);
		View viewDialog=initDialog(R.layout.dialog_cxcard);	
		final RecyclerView ca_rec=(RecyclerView) viewDialog.findViewById(R.id.dc_rec);
		final EditText dc_name=(EditText) viewDialog.findViewById(R.id.dc_name);
		Button dc_ss=(Button) viewDialog.findViewById(R.id.dc_ss);
		final ProgressBar dc_pro=(ProgressBar) viewDialog.findViewById(R.id.dc_pro);
		final TextView dc_null=(TextView) viewDialog.findViewById(R.id.dc_null);
		
		ca_rec.setVisibility(View.GONE);
		dc_pro.setVisibility(View.GONE);
		dc_null.setVisibility(View.VISIBLE);
		
		final List<Card> data=new ArrayList<Card>();
		ca_rec.setLayoutManager(new LinearLayoutManager(context));
		ca_rec.addItemDecoration(new ServiceKacha.MyItemDecoration());
		
		final CardListAdapter cardListAdp=new CardListAdapter(context,data);
		ca_rec.setAdapter(cardListAdp);
		
		//设置回车搜索
		dc_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
					if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {

						String name=dc_name.getText().toString().trim();
					
							ca_rec.setVisibility(View.GONE);
							dc_pro.setVisibility(View.VISIBLE);
							dc_null.setVisibility(View.GONE);

							data.clear();
							List<Card> lc=Gongju.cardQuery(name);
							if(lc.size()!=0){
							cardListAdp.sxAll(lc);
								ca_rec.setVisibility(View.VISIBLE);
								dc_pro.setVisibility(View.GONE);
								dc_null.setVisibility(View.GONE);
							}else{
								ca_rec.setVisibility(View.GONE);
								dc_pro.setVisibility(View.GONE);
								dc_null.setVisibility(View.VISIBLE);	
							}		 
			
				
						gj.closeKeyboard();
						return true;
					}
					return false;
				}
			});
		cardListAdp.setOnCardItemClickListener(new CardListAdapter.OnCardItemClickListener(){

				@Override
				public void onClick(Card card,int position)
				{
					oncard.onClick(card);
					// TODO: Implement this method
				}
			});
		
		dc_ss.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String name=dc_name.getText().toString().trim();
					
						ca_rec.setVisibility(View.GONE);
						dc_pro.setVisibility(View.VISIBLE);
						dc_null.setVisibility(View.GONE);

						data.clear();
						List<Card> lc=Gongju.cardQuery(name);
						if(lc.size()!=0){
							cardListAdp.sxAll(lc);
							ca_rec.setVisibility(View.VISIBLE);
							dc_pro.setVisibility(View.GONE);
							dc_null.setVisibility(View.GONE);
						}else{
							ca_rec.setVisibility(View.GONE);
							dc_pro.setVisibility(View.GONE);
							dc_null.setVisibility(View.VISIBLE);	
						}		 
							

					gj.closeKeyboard();
					// TODO: Implement this method
				}
			});
		
		builder.setCanceledOnTouchOutside(true);
		return new CardList();

	}
	


//下载游戏提示对话框
    public void dialogDygo(){
		
        View viewDialog=initDialog(R.layout.dialog_download_ygo);
		TextView dd_download=(TextView) viewDialog.findViewById(R.id.dd_download);
		TextView dd_qt=(TextView) viewDialog.findViewById(R.id.dd_qt);
		Button dd_qx=(Button) viewDialog.findViewById(R.id.dd_qx);
        
		dd_qx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					// TODO: Implement this method
				}
			});
		dd_qt.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					dialogj1(false,"","查询卡包中,请稍等");
					BmobQuery<GuangGao> bq=new BmobQuery<GuangGao>();			
					bq.addWhereEqualTo("gg_nrlx",0);
					bq.order("-createdAt");	
					bq.findObjects(new FindListener<GuangGao>(){

							@Override
							public void done(final List<GuangGao> gg, BmobException p2)
							{
								if(p2==null){
									dis();
									String[] ss = new String[gg.size()];
									for(int i=0;i<gg.size();i++){
										ss[i]=gg.get(i).getGg_name();
									}
									ListView ll=(ListView) dialogl(true,"请选择要下载的卡包",ss);
									ll.setOnItemClickListener(new OnItemClickListener(){

											@Override
											public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
											{
												dis();
												context.startActivity(IntentUtil.getUrlIntent(context,gg.get(p3).getGg_wz()));
												
												// TODO: Implement this method
											}
										});
								}else{
									dis();
									Gongju.show("查询错误"+p2.getMessage());
								}
								
								// TODO: Implement this method
								}
							});
					
					// TODO: Implement this method
				}
			});
		dd_download.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					dis();
					
					dialogj1(false,"","查询地址中,请稍等");
					BmobQuery<GxWz> bq=new BmobQuery<GxWz>();
					bq.addWhereEqualTo("name","YGOMOBILE");
					bq.findObjects(new FindListener<GxWz>(){

							@Override
							public void done(List<GxWz> p1, BmobException p2)
							{
								if(p2==null){
									if(p1.size()==0){
										Gongju.show("未查询到下载地址");
									}else{
									String wz=p1.get(0).getWz();
									Intent in=new Intent(context,Web.class);
									in.putExtra("title","游戏下载");
									in.putExtra("wz",wz);
									context.startActivity(in);
									}
								}else{
									Gongju.show("查询错误"+p2.getMessage());
								}
								dis();
								// TODO: Implement this method
							}

						});
					
					// TODO: Implement this method
				}
			});
			
		
        builder.setCanceledOnTouchOutside(true);
	}
	
//周围决斗者对话框
	public View[] dialogjdz(boolean bb,String title,final List<RadarNearbyInfo> data){

	    View[] vv=new View[2];
		View viewDialog=initDialog(R.layout.dialog_jdz);
		TextView dj_title=(TextView) viewDialog.findViewById(R.id.dj_title);
		ListView dj_list=(ListView) viewDialog.findViewById(R.id.dj_list);
		Button dj_qd=(Button) viewDialog.findViewById(R.id.dj_qd);
		if(bb){
			dj_title.setText(title);
		}else{
			dj_title.setVisibility(View.GONE);
		}
		dj_list.setAdapter(new BaseAdapter(){

				Zujian zujian;
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  


				class Zujian{
					TextView jdz_name,jdz_sj,jdz_jl;
				}
				@Override
				public int getCount()
				{
					// TODO: Implement this method
					return data.size();
				}

				@Override
				public Object getItem(int p1)
				{
					// TODO: Implement this method
					return data.get(p1);
				}

				@Override
				public long getItemId(int p1)
				{
					// TODO: Implement this method
					return p1;
				}

				@Override
				public View getView(int p1, View p2, ViewGroup p3)
				{
					if(p2==null){
						zujian=new Zujian();
						p2=LayoutInflater.from(context).inflate(R.layout.item_jdz,null);
						zujian.jdz_name=(TextView) p2.findViewById(R.id.jdz_name);
						zujian.jdz_sj=(TextView) p2.findViewById(R.id.jdz_sj);
						zujian.jdz_jl=(TextView) p2.findViewById(R.id.jdz_jl);
						p2.setTag(zujian);

					}else{
						zujian=(Zujian) p2.getTag();
					}

					zujian.jdz_name.setText(data.get(p1).comments);
					zujian.jdz_sj.setText(df.format(data.get(p1).timeStamp));
					String jl="0M";
					double jll=data.get(p1).distance;
					if(jll<1000){
						jl=jll+"M";
					}else{
						jl=jll/1000+"KM";

					}
					zujian.jdz_jl.setText(jl);

					// TODO: Implement this method
					return p2;
				}
			});

		builder.setCanceledOnTouchOutside(true);

		vv[0]=dj_list;
		vv[1]=dj_qd;
		return vv;

	}

		//提示更新对话框
	public View[] dialogAppUpdate(String version,String message){

		View[] v=new View[2];
		View viewDialog=initDialog(R.layout.dialog_app_update);
		Button du_qd=(Button) viewDialog.findViewById(R.id.du_qd);
		Button du_qx=(Button) viewDialog.findViewById(R.id.du_qx);
		TextView du_version=(TextView) viewDialog.findViewById(R.id.du_version_name);
		TextView du_update_message=(TextView) viewDialog.findViewById(R.id.du_update_message);
		
		du_version.setText(version);
		du_update_message.setText(message);
		
		
		v[0]=du_qx;
		v[1]=du_qd;
        builder.setCanceledOnTouchOutside(true);
		return v;	
	}
    
	
	
	
//更新日志对话框
	public View[] dialogupdaterz(boolean bb,String title,final List<Map<String,Object>> data){

	    View[] vv=new View[2];
		View viewDialog=initDialog(R.layout.dialog_updaterz);
		TextView du_title=(TextView) viewDialog.findViewById(R.id.duu_title);
		ListView du_list=(ListView) viewDialog.findViewById(R.id.duu_list);
		Button du_qd=(Button) viewDialog.findViewById(R.id.duu_qd);
		if(bb){
			du_title.setText(title);
		}else{
			du_title.setVisibility(View.GONE);
		}
		du_list.setAdapter(new BaseAdapter(){

				Zujian zujian;
				class Zujian{
					TextView upda_vosin,upda_message;
				}
				@Override
				public int getCount()
				{
					// TODO: Implement this method
					return data.size();
				}

				@Override
				public Object getItem(int p1)
				{
					// TODO: Implement this method
					return data.get(p1);
				}

				@Override
				public long getItemId(int p1)
				{
					// TODO: Implement this method
					return p1;
				}

				@Override
				public View getView(int p1, View p2, ViewGroup p3)
				{
					if(p2==null){
						zujian=new Zujian();
						p2=LayoutInflater.from(context).inflate(R.layout.item_updaterz,null);
						zujian.upda_vosin=(TextView) p2.findViewById(R.id.upda_vosin);
						zujian.upda_message=(TextView) p2.findViewById(R.id.upda_message);

						p2.setTag(zujian);

					}else{
						zujian=(Zujian) p2.getTag();
					}
					zujian.upda_vosin.setText((String)data.get(p1).get("vosin"));
					zujian.upda_message.setText((String)data.get(p1).get("message"));

					// TODO: Implement this method
					return p2;
				}
			});

		builder.setCanceledOnTouchOutside(true);
		vv[0]=du_list;
		vv[1]=du_qd;
		return vv;

	}


//listview对话框
	public ListView dialogl1(boolean bb,String title,final BaseAdapter badp){

		View viewDialog=initDialog(R.layout.dialog_list);
		TextView dl_title=(TextView) viewDialog.findViewById(R.id.dl_title);
		ListView dl_list=(ListView) viewDialog.findViewById(R.id.dl_list);
		if(bb){
			dl_title.setText(title);
		}else{
			dl_title.setVisibility(View.GONE);
		}
		dl_list.setAdapter(badp);

		builder.setCanceledOnTouchOutside(true);
		return dl_list;
		
		
    }
	
//listview对话框
	public View dialogl(boolean bb,String title,final String[] ss){
		
		View viewDialog=initDialog(R.layout.dialog_list);
		TextView dl_title=(TextView) viewDialog.findViewById(R.id.dl_title);
		ListView dl_list=(ListView) viewDialog.findViewById(R.id.dl_list);
		if(bb){
			dl_title.setText(title);
		}else{
			dl_title.setVisibility(View.GONE);
		}
		BaseAdapter b=new BaseAdapter(){

			@Override
			public int getCount()
			{
				return ss.length;//设置此数据适配起有几个item
			}

			@Override
			public Object getItem(int p1)//未知，好像无用
			{
				// TODO: Implement this method
				return ss[p1];
			}

			@Override
			public long getItemId(int p1)//未知，好像无用
			{
				// TODO: Implement this method
				return p1;
			}

			@Override
			public View getView(int p1, View p2, ViewGroup p3)
			{
				//返回的view将是对应的item的布局视图,p1为i数组数据对应的下标
				//注意将ff改成你显示dialog的那个Activity
				TextView t=new TextView(context);
				t.setTextColor(Color.parseColor("#008cf9"));
				t.setText(ss[p1]);//p1为对应的数组下标
				t.setTextSize(20);
				t.setGravity(Gravity.CENTER);
				t.setPadding(6,6,6,6);
				t.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
				return t;//返回设置好的view组件，也可以是布局，也可以是控件
			}
		};
		dl_list.setAdapter(b);

      builder.setCanceledOnTouchOutside(true);
		return dl_list;

	}




//EditText对话框
	public View[] dialoge(boolean bb,String title,String hint){

		View[] v=new View[2];
		View viewDialog=initDialog(R.layout.dialog_edit);
		TextView de_title=(TextView) viewDialog.findViewById(R.id.de_title);
		EditText de_ed=(EditText) viewDialog.findViewById(R.id.de_ed);
		Button de_qd=(Button) viewDialog.findViewById(R.id.de_qd);
		if(bb){
			de_title.setText(title);
		}else{
			de_title.setVisibility(View.GONE);
		}
		de_ed.setHint(hint);
		v[0]=de_ed;
		v[1]=de_qd;
		Gongju.showInputMethod(de_ed,context);
        builder.setCanceledOnTouchOutside(true);
		return v;

	}


//加载对话框
	public View[] dialogj(boolean b,String title,final String neirong){

		View[] v=new View[2];
		View viewDialog=initDialog(R.layout.dialog_jiazai);
		TextView dj_title=(TextView) viewDialog.findViewById(R.id.dj_title);
		 dj_ts=(TextView) viewDialog.findViewById(R.id.dj_ts);
		Button dj_qx=(Button) viewDialog.findViewById(R.id.dj_qx);
		if(b){
			dj_title.setText(title);
		}else{
			dj_title.setVisibility(View.GONE);
		}
		dj_ts.setText(neirong);
		v[0]=dj_qx;
        v[1]=dj_ts;
        builder.setCanceledOnTouchOutside(false);
		return v;

	}

//无按钮加载对话框
    public View[] dialogj1(boolean b,String title,final String neirong){

        View[] v=new View[1];
		View viewDialog=initDialog(R.layout.dialog_jiazai1);
        TextView dj_title=(TextView) viewDialog.findViewById(R.id.dj_title);
        dj_ts=(TextView) viewDialog.findViewById(R.id.dj_ts);
        if(b){
            dj_title.setText(title);
        }else{
            dj_title.setVisibility(View.GONE);
        }
        dj_ts.setText(neirong);
        v[0]=dj_ts;
        builder.setCanceledOnTouchOutside(false);
        return v;

	}
	

//单按钮单图片提示对话框
    public View[] dialogi(boolean b,String title,final String neirong,int im){

        View[] v=new View[1];
        View viewDialog=initDialog(R.layout.dialog_image);
        TextView di_title=(TextView) viewDialog.findViewById(R.id.di_title);
        dj_ts=(TextView) viewDialog.findViewById(R.id.di_ts);
		ImageView di_image=(ImageView) viewDialog.findViewById(R.id.di_image);
		di_image.setImageResource(im);
        Button di_qd=(Button) viewDialog.findViewById(R.id.di_qd);
        if(b){
            di_title.setText(title);
        }else{
            di_title.setVisibility(View.GONE);
        }
        dj_ts.setText(neirong);
        v[0]=di_qd;
       builder.setCanceledOnTouchOutside(true);
        return v;   
	}
	
	
	
//单按钮提示对话框
    public View[] dialogt1(boolean b,String title,final String neirong){

        View[] v=new View[1];
        View viewDialog=initDialog(R.layout.dialog_toast1);
        TextView dt_title=(TextView) viewDialog.findViewById(R.id.dt_title);
        dj_ts=(TextView) viewDialog.findViewById(R.id.dt_ts);
        Button dt_qd=(Button) viewDialog.findViewById(R.id.dt_qd);
        if(b){
            dt_title.setText(title);
        }else{
            dt_title.setVisibility(View.GONE);
        }
        dj_ts.setText(neirong);
        v[0]=dt_qd;
   	   builder.setCanceledOnTouchOutside(true);
        return v;   
	}


	//提示对话框
	public View[] dialogt(boolean b,String title,final String neirong){

		View[] v=new View[2];
		View viewDialog=initDialog(R.layout.dialog_toast);
		TextView dt_title=(TextView) viewDialog.findViewById(R.id.dt_title);
		dj_ts=(TextView) viewDialog.findViewById(R.id.dt_ts);
		Button dt_qd=(Button) viewDialog.findViewById(R.id.dt_qd);
		Button dt_qx=(Button) viewDialog.findViewById(R.id.dt_qx);
		if(b){
			dt_title.setText(title);
		}else{
			dt_title.setVisibility(View.GONE);
		}
		dj_ts.setText(neirong);
		v[0]=dt_qx;
		v[1]=dt_qd;
        builder.setCanceledOnTouchOutside(true);
		return v;	
	}
	
	public void setToast(String s){
		if(dj_ts!=null){
		dj_ts.setText(s);
		}
	}

	public void dengi(){
		yhzc_qd.setText("登录");
		v1.setVisibility(View.VISIBLE);
		v2.setVisibility(View.GONE);
		v3.setVisibility(View.GONE);
		yhzc_lname.setVisibility(View.VISIBLE);
		yhzc_lpassword.setVisibility(View.VISIBLE);
		yhzc_lemail.setVisibility(View.GONE);
		login_v = 1;
	}

	private OnCardItemClickListener oncard;
	public interface OnCardItemClickListener{
		void onClick(Card map);	
	}

	public class CardList{
		public void setOnCardItemClickListener(OnCardItemClickListener oncar){
			oncard=oncar;
		}
	}
}
